// pages/user/user.js
import http from '../../http'
import Dialog from '@vant/weapp/dialog/dialog';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userName: '点击进行登录',
		headUrl: '',
		userId: ''
	},
	gain() {
		http.get({
			url: '/system/auth/get-permission-info',
			then: (res => {
				console.log('用户信息', res.data);
				if (res.data.code == 0) {
					this.setData({
						userName: res.data.data.user.nickname,
						headUrl: res.data.data.user.avatar,
						userId: res.data.data.user.id
					})
				}
			})
		})
	},
	skipLogin() {
		if(!this.data.userId){
			wx.navigateTo({
				url: `../../pages/login/login`
			})
		}
		
	},
	getUserProfile() {
		wx.getUserProfile({
			desc: '用于完善资料',
			success: (res) => {
				console.log('微信用户信息', res);
				let userInfo = res.userInfo
				http.put({
					url: '/system/user/profile/update',
					data: {
						nickname: userInfo.nickName,
						avatar: userInfo.avatarUrl,
						id: this.data.userId
					},
					then: (res) => {
						console.log('修改昵称', res);
						if (res.data.code == 0) {
							wx.downloadFile({
								url: userInfo.avatarUrl,
								success: (res) => {
									console.log('存储图片地址', res);
									if (res.statusCode === 200) {
										let imgUrl = res.tempFilePath
										http.file({
											url: '/system/user/profile/update-avatar',
											data: {},
											img: imgUrl,
											then: (res) => {
												console.log('修改头像', res);
												let data = JSON.parse(res.data)
												if (data.code == 0) {
													wx.showToast({
														title: '更新成功',
														icon: 'error',
													})
													this.gain()
												} else {
													wx.showToast({
														title: data.msg,
														icon: 'error',
													})
												}
											}
										})
									}
								}
							})
						}
					}
				})
			}
		})
	},
	take() {
		wx.requestSubscribeMessage({
			tmplIds: ['3zK1Iyi1nK9hMam_R6RjLeVH9eH238rQwOI46blrZYU'], // 你的订阅消息模板ID数组
			success(res) {
				console.log(res);
				if (res['3zK1Iyi1nK9hMam_R6RjLeVH9eH238rQwOI46blrZYU'] === 'accept') {
					console.log('用户同意了订阅消息');
					wx.login({
						success: (loginRes) => {
							if (loginRes.code) {
								http.post({
									url: '/zg/applet/saveSubscription',
									data: {
										code: loginRes.code,
										templateId: '3zK1Iyi1nK9hMam_R6RjLeVH9eH238rQwOI46blrZYU'
									},
									then: (res) => {
										console.log('订阅消息', res);
										Dialog.alert({
											title: '订阅消息',
											message: '您已成功订阅,取消订阅请在右上角设置中进行修改',
										}).then(() => {});
									}
								})
							}
						},
					})
				} else {
					Dialog.alert({
						title: '订阅消息',
						message: '您已设置拒绝订阅,如需订阅请在右上角设置中进行修改',
					}).then(() => {});
				}
			},
			fail(err) {
				console.error(err);
				if (err.errCode == 200004) {
					Dialog.alert({
						title: '订阅消息',
						message: '用户关闭了主开关，无法进行订阅,如需订阅请在右上角设置中进行修改',
					}).then(() => {});
				}
			}
		})
	},
	meInfo() {
		wx.navigateTo({
			url: `../../user/meInfo/meInfo`
		})
	},
	issue() {
		wx.navigateTo({
			url: `../../user/issue/issue`
		})
	},
	set() {
		wx.navigateTo({
			url: `../../user/set/set`
		})
	},
	quit() {
		Dialog.confirm({
				title: '您确定要退出登陆吗?',
			})
			.then(() => {
				wx.removeStorageSync('token')
				this.setData({
					userName: '点击进行登录',
					headUrl: '',
					userId: ''
				}, () => {
					wx.redirectTo({
						url: '../../pages/login/login',
					})
				})
			})

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.gain()

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})